/*

Copyright 2007 Renaun Erickson (http://renaun.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

@ignore
*/
package asfslib.services.familytree
{
import asfslib.FamilySearchService;
import asfslib.api.familytree.v1.Person;
import asfslib.events.FamilySearchServiceEvent;
import asfslib.services.BaseService;

import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;

import mx.collections.ArrayCollection;

/**
 * Fired when a result is obtain from the respective service call.
 */
[Event(name="read", type="asfslib.events.PersonServiceEvent")]

/**
 * Fired when a result is obtain from the respective service call.
 */
[Event(name="readMultiple", type="asfslib.events.PersonServiceEvent")]
	
public class PersonService extends BaseService
{

    //--------------------------------------------------------------------------
    //
    //  Constants
    //
    //--------------------------------------------------------------------------

	/**
	 * 	Constant view parameter assertions
	 */
	public static const VIEW_ASSERTIONS:String = "assertions";
	
	/**
	 * 	Constant view parameter composition
	 */
	public static const VIEW_COMPOSITION:String = "composition";
	
	/**
	 * 	Constant view parameter information
	 */
	public static const VIEW_INFORMATION:String = "information";
	
	/**
	 * 	Constant view parameter summary
	 */
	public static const VIEW_SUMMARY:String = "summary";
	
	/**
	 * 	Constant view parameter version
	 */
	public static const VIEW_VERSION:String = "version";
	
	/**
	 * 	Constant view parameter values
	 */
	public static const VIEW_VALUES:String = "values";

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

	/**
	 * 	Constructor
	 */
	public function PersonService(service:FamilySearchService)
	{
		super(service);
		
		var type:String = FamilySearchService.FAMILY_TREE_SERVICES;
		var ver:String = FamilySearchService.FAMILY_TREE_VERSION_STRING;
		var name:String = FamilySearchService.PERSON_SERVICE;
		endpoint = service.familySearchURL + type + "/" + ver + "/" + name
	}	

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

 	//----------------------------------
    //  views
    //----------------------------------

    /**
     *  @private
     *  Storage for the views property.
     */
    private var _views:Array = ["assertions","information"];

    /**
     *  The views property
     *
     *  @default ["assertions","information"]
     */
    public function get views():Array
    {
        return _views;
    }

    /**
     *  @private
     */
    public function set views(value:Array):void
    {
        _views = value;
    }
 	
 	//----------------------------------
    //  contributor
    //----------------------------------

    /**
     *  @private
     *  Storage for the contributor property.
     */
    private var _contributor:String;

    /**
     *  The contributor property.
     * 
     *  This parameter is valid with the assertions view only.
     *
     *  @default 
     */
    public function get contributor():String
    {
    	for each(var value:String in views)
    		if (value == "assertions")
    			return _contributor;
    	return null;
    }

    /**
     *  @private
     */
    public function set contributor(value:String):void
    {
        _contributor = value;
    }
    
    //----------------------------------
    //  startIndex
    //----------------------------------

    /**
     *  @private
     *  Storage for the startIndex property.
     */
    private var _startIndex:int;

    /**
     *  The startIndex property
     *
     *  @default 
     */
    public function get startIndex():int
    {
        return _startIndex;
    }

    /**
     *  @private
     */
    public function set startIndex(value:int):void
    {
        _startIndex = value;
    }


    //----------------------------------
    //  maxResults
    //----------------------------------

    /**
     *  @private
     *  Storage for the maxResults property.
     */
    private var _maxResults:int;

    /**
     *  The maxResults property
     *
     *  @default 
     */
    public function get maxResults():int
    {
        return _maxResults;
    }

    /**
     *  @private
     */
    public function set maxResults(value:int):void
    {
        _maxResults = value;
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------
    
    /**
     *	Search for people in the FamilySearch database.
     *  Default is to just search on the givenName, all other optional parameters
     *  have to be set before making the call.
     */ 
    public function read(id:String):void
    {
		// TODO Build Query String off all parameters.
        var variables:URLVariables = new URLVariables();
        variables.view = ["assertions","composition"];
        variables.sessionId = service.sessionID;
        
		var urlRequest:URLRequest = new URLRequest(endpoint + "/" + id + "?" + variables.toString());
		urlRequest.method = URLRequestMethod.GET;

		serviceLoader.load(urlRequest);
    }
    
    /**
     *	Override the completeHandler
     */
    override protected function completeHandler(event:Event):void
    {
    	var loader:URLLoader = event.target as URLLoader;
    	var resultXML:XML = XML(loader.data);

		if (checkFSAPIErrors(resultXML))
			return;
		
		var results:ArrayCollection = parseResults(resultXML);
		
		var eventType:String = FamilySearchServiceEvent.READ_MULTIPLE;
		var result:Object = results;
		if (results.length == 1)
		{
			eventType = FamilySearchServiceEvent.READ;
			result = results[0];
		}
		
		var serviceEvent:FamilySearchServiceEvent = new FamilySearchServiceEvent(
															eventType,
															false,
															false,
															result);
		
		dispatchEvent(serviceEvent);
    }
    
    /**
     *	Parse the result XML and create the Objects
     */
    private function parseResults(resultXML:XML):ArrayCollection
    {
		if (resultXML.namespace("") != undefined) 
		{
		    default xml namespace = resultXML.namespace("");
		}    	
    	var people:ArrayCollection = new ArrayCollection();
    	  	
    	for each(var resultNode:XML in resultXML.persons.person)
    	{
    		people.addItem(Person.parseXML(resultNode));
    	}
    	
    	default xml namespace = new Namespace("");
    	return people;
    }
}
}